package com.thinkgem.jeesite.common.utils;

import com.thinkgem.jeesite.common.config.Global;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class ImgUtils {

    /**
     * 获取图片宽度 高度
     * @param path  图片文件路径
     * @return map
     */
    public static Map<String,String> getImgWidthHeight(String path) {

        InputStream is = null;
        BufferedImage src = null;
        int width = -1;
        int height = -1;
        try {
            File file = new File(URLDecoder.decode(Global.getUserfilesBaseDir() + path, "UTF-8"));
            is = new FileInputStream(file);
            src = javax.imageio.ImageIO.read(is);
            width = src.getWidth(null); // 得到源图宽
            height = src.getHeight(null); // 得到源图高
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String,String> ret=new HashMap();
        ret.put("height",height+"");
        ret.put("width",width+"");
        return ret;
    }


}
